import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {BaseInputComponent} from "@shared/base-form/base-input/base-input.component";

@Component({
  selector: 'base-array-input',
  templateUrl: './base-array-input.component.html',
})
export class BaseArrayInputComponent extends BaseInputComponent implements OnInit{

  /*READEME:配置值如；  6,5,5*/
  public inputVisible = false;
  public inputValue = '';
  @ViewChild('input') input: ElementRef;

  constructor() {
    super();
  }
  ngOnInit() {
    this.entity.valueArr = this.entity.value ? this.entity.value.split(",") : [];
  }

  /*array*/
  handleClose(removedTag: any): void {
    this.entity.valueArr = this.entity.valueArr.filter(tag => tag !== removedTag);
    this.entity.value = this.entity.valueArr.join(",");
    this.validateForm.controls.value.setValue( this.entity.value);
  }

  showInput(): void {
    this.inputVisible = true;
    setTimeout(() => {
      this.input.nativeElement.focus();
    }, 10);
  }

  handleInputConfirm(): void {
    if (this.inputValue) {
      let findex = this.entity.valueArr.findIndex((v) => {
        return this.inputValue == v;
      });//判断排重
      if (findex == -1) {
        this.entity.valueArr.push(this.inputValue);
        this.entity.value = this.entity.valueArr.join(",");
        this.validateForm.controls.value.setValue( this.entity.value);
      }
    }
    this.inputValue = '';
    this.inputVisible = false;
  }



}
